#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition, UnlessCondition

def generate_launch_description():
    # 声明启动参数
    start_publisher_arg = DeclareLaunchArgument(
        'start_publisher',
        default_value='true',
        description='Whether to start publisher node'
    )

    start_subscriber_arg = DeclareLaunchArgument(
        'start_subscriber',
        default_value='true',
        description='Whether to start subscriber node'
    )

    start_service_arg = DeclareLaunchArgument(
        'start_service',
        default_value='false',
        description='Whether to start service nodes'
    )

    # 创建节点
    publisher_node = Node(
        package='multi_launch_demo',
        executable='publisher_node',
        name='publisher_node',
        parameters=[{
            'publish_frequency': 2.0,
            'message_prefix': 'Conditional'
        }],
        output='screen',
        condition=IfCondition(LaunchConfiguration('start_publisher'))
    )

    subscriber_node = Node(
        package='multi_launch_demo',
        executable='subscriber_node',
        name='subscriber_node',
        output='screen',
        condition=IfCondition(LaunchConfiguration('start_subscriber'))
    )

    service_server_node = Node(
        package='multi_launch_demo',
        executable='service_server_node',
        name='service_server_node',
        output='screen',
        condition=IfCondition(LaunchConfiguration('start_service'))
    )

    service_client_node = Node(
        package='multi_launch_demo',
        executable='service_client_node',
        name='service_client_node',
        output='screen',
        condition=IfCondition(LaunchConfiguration('start_service'))
    )

    param_node = Node(
        package='multi_launch_demo',
        executable='param_node',
        name='param_node',
        parameters=[{
            'param_string': 'Conditional launch demo',
            'param_int': 999,
            'param_double': 9.99,
            'param_bool': True
        }],
        output='screen'
    )

    # 日志信息
    log_info = LogInfo(
        msg="Conditional launch demo started",
        condition=IfCondition(LaunchConfiguration('start_publisher'))
    )

    return LaunchDescription([
        start_publisher_arg,
        start_subscriber_arg,
        start_service_arg,
        publisher_node,
        subscriber_node,
        service_server_node,
        service_client_node,
        param_node,
        log_info
    ])
